<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getTaskMonthStatic } from "api/home";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "250px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    selectedDate: {
      type: Object,
      required: true
    },
    dateRange: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      xData: [],
      monthData: []
    };
  },
  watch: {
    selectedDate: {
      deep: true,
      handler(val) {
        this.loadData();
      }
    },
    dateRange() {
      this.setData(this.monthData);
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.loadData();
      this.setOptions();
    },
    loadData() {
      getTaskMonthStatic(this.selectedDate).then(res => {
        this.monthData = res.data;
        this.setData(res.data);
      });
    },
    setData(data) {
      let range = 15;
      let xData =
        this.dateRange == 1
          ? data.slice(0, range)
          : data.slice(range, data.length);
      this.xData = xData.map(o => o.cdate);
      let chartData = {
        expectedData: xData.map(o => o.totalNum),
        actualData: xData.map(o => o.finishedNum)
      };
      this.setOptions(chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.xData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ["目标值", "完成值"]
        },
        series: [
          {
            name: "目标值",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2
                }
              }
            },
            smooth: true,
            type: "line",
            data: expectedData,
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          },
          {
            name: "完成值",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2
                },
                areaStyle: {
                  color: "#f3f8ff"
                }
              }
            },
            data: actualData,
            animationDuration: 2800,
            animationEasing: "quadraticOut"
          }
        ]
      });
    }
  }
};
</script>
